package com.vx.vxbug.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vx.vxbug.dao.IBugDao;
import com.vx.vxbug.entities.Bug;

@Service
public class BugService {

	@Autowired
	private IBugDao BugDao;

    @Autowired
    private UserService uS;

    @Autowired
    private ProgramService pS;

    @Autowired
    private SendMailService sm;

	public void addBug(Bug b) {

		if (!b.getOpenerUserName().equals("anonymousUser")){
            b.setOpenerUserId(uS.getUserByLogin(b.getOpenerUserName()).getId());
        }
        BugDao.addBug(b);
        String to = uS.getUserById(pS.getProgramById(b.getProgramId()).getCreatorUserId()).getEmail();

        sm.sendEmail(to ,"New bug was found in " + pS.getProgramById(b.getProgramId()).getName() +"!", "You can check it out at\nhttp://quarcum.dyndns.org:8087/vxBug/program/" + b.getProgramId() + "/bug/" + b.getId());
	}

	public Bug getBugById(int id) {
		return BugDao.getBugById(id);
	}

	public List<Bug> getAllBugsForProgramId(int id) {
		return BugDao.getAllBugsForProgramId(id);
	}

    public void update(Bug b){
        BugDao.updateBug(b);
    }
}
